package server.commands;

import client.MapleCharacter;
import client.MapleClient;
import client.MapleStat;
import client.inventory.Item;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import configs.ServerConfig;
import constants.ItemConstants;
import constants.JobConstants;
import constants.ServerConstants;
import handling.channel.handler.InterServerHandler;
import handling.world.World;
import handling.world.WorldFindService;
import scripting.item.ItemScriptManager;
import scripting.npc.NPCScriptManager;
import scripting.quest.QuestScriptManager;
import server.MapleInventoryManipulator;
import server.MapleItemInformationProvider;
import server.RankingWorker;
import server.RankingWorker.RankingInformation;
import server.life.MapleMonster;
import server.maps.MapleMap;
import server.maps.MapleMapObject;
import server.maps.MapleMapObjectType;
import server.maps.SavedLocationType;
import tools.DateUtil;
import tools.MaplePacketCreator;
import tools.Randomizer;
import tools.StringUtil;
import tools.packet.UIPacket;

import java.util.Collections;
import java.util.List;

/**
 * @author Emilyx3
 */
public class PlayerCommand {

    /**
     * @return
     */
    public static PlayerGMRank getPlayerLevelRequired() {
        return PlayerGMRank.NORMAL;
    }


    public static class STR extends DistributeStatCommands {


        public STR() {
            stat = MapleStat.力量;
        }
    }


    public static class DEX extends DistributeStatCommands {


        public DEX() {
            stat = MapleStat.敏捷;
        }
    }


    public static class INT extends DistributeStatCommands {


        public INT() {
            stat = MapleStat.智力;
        }
    }


    public static class LUK extends DistributeStatCommands {


        public LUK() {
            stat = MapleStat.运气;
        }
    }

    public static class HP extends DistributeStatCommands {


        public HP() {
            stat = MapleStat.MAXHP;
        }
    }

    public static class MP extends DistributeStatCommands {


        public MP() {
            stat = MapleStat.MAXMP;
        }
    }


    public abstract static class DistributeStatCommands extends CommandExecute {


        protected MapleStat stat = null;

        private void setStat(MapleCharacter player, int amount) {
            switch (stat) {
                case 力量:
                    player.getStat().setStr((short) amount, player);
                    player.updateSingleStat(MapleStat.力量, player.getStat().getStr());
                    break;
                case 敏捷:
                    player.getStat().setDex((short) amount, player);
                    player.updateSingleStat(MapleStat.敏捷, player.getStat().getDex());
                    break;
                case 智力:
                    player.getStat().setInt((short) amount, player);
                    player.updateSingleStat(MapleStat.智力, player.getStat().getInt());
                    break;
                case 运气:
                    player.getStat().setLuk((short) amount, player);
                    player.updateSingleStat(MapleStat.运气, player.getStat().getLuk());
                    break;
                case MAXHP:
                    amount =  Math.min(player.getMaxHpForSever(), Math.abs(amount));
                    player.getStat().setMaxHp(amount, player);
                    player.updateSingleStat(MapleStat.MAXHP, player.getStat().getMaxHp());
                    break;
                case MAXMP:
                    amount =  Math.min(player.getMaxMpForSever(), Math.abs(amount));
                    player.getStat().setMaxMp(amount, player);
                    player.updateSingleStat(MapleStat.MAXMP, player.getStat().getMaxMp());
                    break;
            }
        }

        private int getStat(MapleCharacter player) {
            switch (stat) {
                case 力量:
                    return player.getStat().getStr();
                case 敏捷:
                    return player.getStat().getDex();
                case 智力:
                    return player.getStat().getInt();
                case 运气:
                    return player.getStat().getLuk();
                case MAXHP:
                    return player.getStat().getMaxHp();
                case MAXMP:
                    return player.getStat().getMaxMp();
                default:
                    throw new RuntimeException(); //Will never happen.
            }
        }

        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (splitted.length < 2) {
                c.getPlayer().dropMessage(5, "使用方式 @STR/DEX/INT/LUK/HP/MP 數字(負的需使用 洗能力點卷軸).");
                return 0;
            }
            int change, changestate;
            try {
                change = Integer.parseInt(splitted[1]);
                changestate = change;
            } catch (NumberFormatException nfe) {
                c.getPlayer().dropMessage(1, "輸入的數字無效.");
                return 0;
            }
            if (stat.name() == "MAXHP" || stat.name() == "MAXMP") {
                if (getStat(c.getPlayer()) == 500000) {
                    c.getPlayer().dropMessage(1, "您的血(魔)已到達上限.");
                    return 0;
                }
            }
            if (JobConstants.isNotMpJob(c.getPlayer().getJob()) && stat.name() == "MAXMP") {  //恶魔和天使不能洗魔力
                c.getPlayer().dropMessage(1, "您無法洗魔力.");
                return 0;
            }
            if (change <= 0) {
                if(getStat(c.getPlayer()) <= 4){
                    c.getPlayer().dropMessage(1, "能力點不能小於4.");
                    return 0;
                }
                if (stat.name() == "MAXHP" || stat.name() == "MAXMP") {
                    if(c.getPlayer().getHpApUsed() <= 0){
                        c.getPlayer().dropMessage(1, "您在HP與MP沒有投注過任何能力點.");
                        return 0;
                    }
                    if(c.getPlayer().getHpApUsed() - Math.abs(change) < 0){
                        c.getPlayer().dropMessage(1, "您只能輸入不小於 -" + (c.getPlayer().getHpApUsed()) + " 的數字");
                        return 0;
                    }
                    if (getStat(c.getPlayer()) <= 50) {
                        c.getPlayer().dropMessage(1, "您的血(魔)已到達最低.");
                        return 0;
                    }
                    changestate = 0;
                    for(int i  = 1; i <= Math.abs(change);i++) {
                        changestate -= Randomizer.rand(15, 25);
                    }
                }
                if(!c.getPlayer().haveItem(5050000, Math.abs(change))){
                    c.getPlayer().dropMessage(1, "您沒有足夠的 洗能力點卷軸.");
                    return 0;
                }
                if (MapleInventoryManipulator.removeById(c, ItemConstants.getInventoryType(5050000), 5050000, -change, false, false)) {
                    c.announce(MaplePacketCreator.getShowItemGain(5050000, (short) -change, true));
                }else {
                    return 0;
                }
            }else{
                if(stat.name() == "MAXHP" || stat.name() == "MAXMP"){
                    changestate = 0;
                    for(int i  = 1; i <= Math.abs(change);i++) {
                        changestate += Randomizer.rand(50, 80);
                    }
                }
            }
            if (c.getPlayer().getRemainingAp() < change) {
                c.getPlayer().dropMessage(1, "您的能力點不足.");
                return 0;
            }
            if (!(stat.name() == "MAXHP") && !(stat.name() == "MAXMP")) {
                if (getStat(c.getPlayer()) + change > ServerConfig.CHANNEL_PLAYER_MAXAP) {
                    c.getPlayer().dropMessage(1, "所要分配後的能力總和不可大於 " + ServerConfig.CHANNEL_PLAYER_MAXAP + " 點.");
                    return 0;
                }
            }else{
                if(getStat(c.getPlayer()) + changestate <= 50)
                    changestate = getStat(c.getPlayer()) - 50;
            }
            if(stat.name() == "MAXHP" || stat.name() == "MAXMP"){
                c.getPlayer().setHpApUsed((short) (c.getPlayer().getHpApUsed() + change));
            }
            setStat(c.getPlayer(), getStat(c.getPlayer()) + changestate);
            c.getPlayer().setRemainingAp((short) (c.getPlayer().getRemainingAp() - change));
            c.getPlayer().updateSingleStat(MapleStat.AVAILABLEAP, c.getPlayer().getRemainingAp());
            c.getPlayer().dropMessage(5, "加(扣)點成功 您的 " + StringUtil.makeEnumHumanReadable(stat.name()) + " 提高(減少)了 " + changestate + " 點.");
            return 1;
        }
    }

    /**
     * 角色存挡命令
     */

    public static class Save extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (c.getPlayer().getCheatTracker().canSaveDB()) {
                c.getPlayer().dropMessage(5, "開始保存角色數據...");
                c.getPlayer().saveToCache();
                c.getPlayer().dropMessage(5, "保存角色數據完成...");
                return 1;
            } else {
                c.getPlayer().dropMessage(5, "保存角色數據失敗，此指令使用間隔時間為60秒。");
                return 0;
            }
        }
    }

    /**
     * 角色复活命令
     */


//    public static class Fh extends CommandExecute {
//
//        @Override
//        public int execute(MapleClient c, String[] splitted) {
//            int reliveCount = 5;
//            int reliveCost = 50000;
//            boolean usemPoints = reliveCost <= 10000;
//            boolean canRelive = false;
//            if (c.getPlayer().getLevel() < 70) {
//                c.getPlayer().dropMessage(5, "等级达到70级才可以使用这个命令.");
//                return 0;
//            }
//            if (c.getPlayer().isAlive()) {
//                c.getPlayer().dropMessage(5, "您都還没有挂掉，怎么能使用这个命令呢。");
//                return 0;
//            }
//            if (c.getPlayer().getBossLog("原地复活") >= reliveCount) {
//                if (usemPoints) {
//                    canRelive = c.getPlayer().getCSPoints(2) > reliveCost;
//                } else {
//                    canRelive = c.getPlayer().getMeso() > reliveCost;
//                }
//            } else {
//                canRelive = true;
//            }
//            if (!canRelive) {
//                c.getPlayer().dropMessage(5, "您今天的免费复活次数已经用完或者您的" + (usemPoints ? "抵用券" : "金币") + "不足" + reliveCost);
//                return 0;
//            }
//            String message = "恭喜您原地复活成功，";
//            if (c.getPlayer().getBossLog("原地复活") < reliveCount) {
//                message += "您今天還可以免费使用: " + (reliveCount - c.getPlayer().getBossLog("原地复活")) + " 次。";
//                c.getPlayer().setBossLog("原地复活");
//            } else {
//                message += "本次复活花费 " + reliveCost + (usemPoints ? " 抵用券" : " 金币");
//                if (usemPoints) {
//                    c.getPlayer().modifyCSPoints(2, -reliveCost);
//                } else {
//                    c.getPlayer().gainMeso(-reliveCost, true);
//                }
//            }
//            c.getPlayer().getStat().heal(c.getPlayer());
//            c.getPlayer().dispelDebuffs();
//            //c.getPlayer().instantMapWarp(0);
//            c.getPlayer().setStance(0);
////            MapleMap to = c.getPlayer().getMap();
////            c.getPlayer().changeMap(to, to.getPortal(0));
//            c.getPlayer().dropMessage(5, message);
//            return 0;
//        }
//    }

    /*
     * 角色复活命令
     */

//    public static class vipFH extends CommandExecute {
//
//        @Override
//        public int execute(MapleClient c, String[] splitted) {
//            int reliveCount = c.getPlayer().getItemQuantity(c.getChannelServer().getReliveItem());
//            boolean canRelive = reliveCount > 1;
//            if (c.getPlayer().isAlive()) {
//                c.getPlayer().dropMessage(5, "您都還没有挂掉，怎么能使用这个命令呢。");
//                return 0;
//            }
//            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
//            if (!canRelive) {
//                c.getPlayer().dropMessage(5, "您的 " + (ii.getName(c.getChannelServer().getReliveItem())) + " 不足1个。");
//                return 0;
//            }
//            c.getPlayer().removeItem(c.getChannelServer().getReliveItem(), 1);
//            c.getPlayer().getStat().heal(c.getPlayer());
//            c.getPlayer().dispelDebuffs();
//            c.getPlayer().setStance(0);
//            c.getPlayer().dropMessage(5, "恭喜您原地复活成功，当前剩余次数 " + (reliveCount - 1));
//            return 0;
//        }
//    }

    /*
     * 查看怪物信息
     */

    public static class Mob extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            MapleMonster mob = null;
            for (MapleMapObject monstermo : c.getPlayer().getMap().getMapObjectsInRange(c.getPlayer().getPosition(), 100000, Collections.singletonList(MapleMapObjectType.MONSTER))) {
                mob = (MapleMonster) monstermo;
                if (mob.isAlive()) {
                    if(mob.getId() == 9300216)
                        continue;
                    c.getPlayer().dropMessage(6, "怪物: " + mob.toString());
                    break; //只能查看1个怪物的信息
                }
            }

            if (mob == null) {
                c.getPlayer().dropMessage(6, "查看失敗: 1.沒有找到需要查看的怪物信息. 2.你周圍沒有怪物出現. 3.有些怪物禁止查看.");
            }
            return 1;
        }
    }

    /*
     * 传送到自由市场
     */


    public static class ziyou extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (ServerConstants.isBlockedMapFM(c.getPlayer().getMapId())) {
                c.getPlayer().dropMessage(5, "当前地图禁止使用此命令");
                return 0;
            }
            if (c.getPlayer().getLevel() < 10 && c.getPlayer().getJob() != 200) {
                c.getPlayer().dropMessage(5, "等级达到10才能使用此命令");
                return 0;
            }
            if (c.getPlayer().hasBlockedInventory() || c.getPlayer().getMap().getSquadByMap() != null || c.getPlayer().getEventInstance() != null || c.getPlayer().getMap().getEMByMap() != null || c.getPlayer().getMapId() >= 990000000) {
                c.getPlayer().dropMessage(5, "当前地图禁止使用此命令");
                return 0;
            }
            if ((c.getPlayer().getMapId() >= 680000210 && c.getPlayer().getMapId() <= 680000502) || (c.getPlayer().getMapId() / 1000 == 980000 && c.getPlayer().getMapId() != 980000000) || (c.getPlayer().getMapId() / 100 == 1030008) || (c.getPlayer().getMapId() / 100 == 922010) || (c.getPlayer().getMapId() / 10 == 13003000)) {
                c.getPlayer().dropMessage(5, "当前地图禁止使用此命令");
                return 0;
            }
            c.getPlayer().saveLocation(SavedLocationType.FREE_MARKET, c.getPlayer().getMap().getReturnMap().getId());
            MapleMap map = c.getChannelServer().getMapFactory().getMap(910000000);
            c.getPlayer().changeMap(map, map.getPortal(0));
            return 1;
        }
    }


//    public static class PM extends CommandExecute {
//
//        @Override
//        public int execute(MapleClient c, String[] splitted) {
//            InterServerHandler.EnterMTS(c, c.getPlayer());
//            return 1;
//        }
//    }

    /*
     * 角色解卡
     */


    public static class EA extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            c.removeClickedNPC();
            NPCScriptManager.getInstance().dispose(c);
            ItemScriptManager.getInstance().dispose(c);
            QuestScriptManager.getInstance().dispose(c);
            c.announce(MaplePacketCreator.enableActions());
            c.getPlayer().dropMessage(5, "解卡完成!");
            return 1;
        }
    }

    /*
     * 查看游戏排名
     */


//    public static class Ranking extends CommandExecute {
//
//        @Override
//        public int execute(MapleClient c, String[] splitted) {
//            if (splitted.length < 4) { //job start end
//                c.getPlayer().dropMessage(5, "使用 @ranking [职业类型] [起始排名] [结束排名] 例子:[@ranking 所有 1 20]此为显示所有职业排名第1-20的信息");
//                StringBuilder builder = new StringBuilder("职业类型: ");
//                for (String b : RankingWorker.getJobCommands().keySet()) {
//                    builder.append(b);
//                    builder.append(" ");
//                }
//                c.getPlayer().dropMessage(5, builder.toString());
//            } else {
//                int start = 1, end = 20;
//                try {
//                    start = Integer.parseInt(splitted[2]);
//                    end = Integer.parseInt(splitted[3]);
//                } catch (NumberFormatException e) {
//                    c.getPlayer().dropMessage(5, "输入的显示排名数字错误.每次只能显示20个角色的信息 例子:[@ranking 所有 1 20");
//                }
//                if (end < start || end - start > 20) {
//                    c.getPlayer().dropMessage(5, "输入的显示排名数字错误.每次只能显示20个角色的信息 例子:[@ranking 所有 1 20");
//                } else {
//                    Integer job = RankingWorker.getJobCommand(splitted[1]);
//                    if (job == null) {
//                        c.getPlayer().dropMessage(5, "输入的职业类型代码不存在.");
//                    } else {
//                        List<RankingInformation> ranks = RankingWorker.getRankingInfo(job);
//                        if (ranks == null || ranks.size() <= 0) {
//                            c.getPlayer().dropMessage(5, "請稍后在试.");
//                        } else {
//                            int num = 0;
//                            for (RankingInformation rank : ranks) {
//                                if (rank.rank >= start && rank.rank <= end) {
//                                    if (num == 0) {
//                                        c.getPlayer().dropMessage(6, "当前显示为 " + splitted[1] + " 的排名 - 开始 " + start + " 结束 " + end);
//                                        c.getPlayer().dropMessage(6, "--------------------------------------------");
//                                    }
//                                    c.getPlayer().dropMessage(6, rank.toString());
//                                    num++;
//                                }
//                            }
//                            if (num == 0) {
//                                c.getPlayer().dropMessage(5, "排名信息为空.");
//                            }
//                        }
//                    }
//                }
//            }
//            return 1;
//        }
//    }


    /*
     * 穿戴宝盒
     */


    // public static class 穿戴寶盒 extends CommandExecute {

    //     @Override
    //     public int execute(MapleClient c, String[] splitted) {
    //         if (splitted.length < 2) {
    //             c.getPlayer().dropMessage(6, "用法: @穿戴寶盒 < 寶盒在裝備欄的位置 >");
    //             return 0;
    //         }
    //         if (c.getPlayer().getLevel() < 10) {
    //             c.getPlayer().dropMessage(5, "等級達到 10 才可使用此指令.");
    //             return 0;
    //         }
    //         if (JobConstants.is龙的传人(c.getPlayer().getJob())) {
    //             short src = (short) Integer.parseInt(splitted[1]);
    //             Item toUse = c.getPlayer().getInventory(MapleInventoryType.EQUIP).getItem(src);
    //             if (toUse == null || toUse.getQuantity() < 1 || !ItemConstants.is龙传宝盒(toUse.getItemId())) {
    //                 c.getPlayer().dropMessage(6, "穿戴錯誤，裝備欄第 " + src + " 個道具不存在，或者該道具不是寶盒裝備。");
    //                 return 0;
    //             }
    //             MapleInventoryManipulator.equip(c, src, (short) -10);
    //             return 1;
    //         } else {
    //             c.getPlayer().dropMessage(6, "此指令只對龍魔導開放。");
    //             return 0;
    //         }
    //     }
    // }

    /*
     * 取下宝盒
     */


    // public static class 取下寶盒 extends CommandExecute {

    //     @Override
    //     public int execute(MapleClient c, String[] splitted) {
    //         if (JobConstants.is龙的传人(c.getPlayer().getJob())) {
    //             Item toUse = c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).getItem((short) -10);
    //             if (toUse == null || c.getPlayer().getInventory(MapleInventoryType.EQUIP).isFull()) {
    //                 c.getPlayer().dropMessage(6, "取下寶盒錯誤，無裝備副武器，或者裝備欄已滿。");
    //                 return 0;
    //             }
    //             MapleInventoryManipulator.unequip(c, (byte) -10, c.getPlayer().getInventory(MapleInventoryType.EQUIP).getNextFreeSlot());
    //             return 1;
    //         } else {
    //             c.getPlayer().dropMessage(6, "此指令只對龍魔導開放。");
    //             return 0;
    //         }
    //     }
    // }

    /*
     * 删除盾牌
     */


    public static class DelS extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (JobConstants.is恶魔(c.getPlayer().getJob())) {
                Item toRemove = c.getPlayer().getInventory(MapleInventoryType.EQUIPPED).getItem((short) -10);
                if (toRemove != null && toRemove.getItemId() / 1000 == 1099 && toRemove.getItemId() != 1099004 && toRemove.getItemId() != 1099005 && toRemove.getItemId() != 1099009) {
                    MapleInventoryManipulator.removeFromSlot(c, MapleInventoryType.EQUIPPED, toRemove.getPosition(), toRemove.getQuantity(), false);
                    c.getPlayer().equipChanged();
                    return 1;
                } else {
                    c.getPlayer().dropMessage(6, "刪除盾牌失敗，無裝備副武器或盾牌.或者該盾牌已可手動脫除.");
                    return 0;
                }
            } else {
                c.getPlayer().dropMessage(6, "此指令只對惡魔職業開放。");
                return 0;
            }
        }
    }


    // public static class 怪怪 extends CommandExecute {

    //     @Override
    //     public int execute(MapleClient c, String[] splitted) {
    //         c.announce(UIPacket.sendOpenWindow(0x254));
    //         return 1;
    //     }
    // }

    // public static class Online extends CommandExecute {

    //     @Override
    //     public int execute(MapleClient c, String[] splitted) {
    //         java.util.Map<Integer, Integer> connected = World.getConnected();
    //         c.getPlayer().dropMessage(6, "--------------------------伺服器列表-------------------------");
    //         for (int i : connected.keySet()) {
    //             if (i == 0) {
    //                 c.getPlayer().dropMessage(6, "總伺服器人數 :" + connected.get(i) + "人");
    //             } else {
    //                 if(i <= 0)
    //                     continue;
    //                 c.getPlayer().dropMessage(6, "頻道 "+ i +" : " + connected.get(i) + "人");
    //             }
    //         }
    //         c.getPlayer().dropMessage(6, "-------------------------------------------------------------");
    //         return 1;
    //     }
    // }
    public static class CheckDrop extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (c.getPlayer().getLevel() < 10) {
                c.getPlayer().dropMessage(5, "等级達到10级才可以使用這個命令.");
                return 0;
            }
            NPCScriptManager.getInstance().start(c, 9270035);
            return 1;
        }
    }
    public static class Chalk extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            StringBuilder sb = new StringBuilder();

            for(int i = 1;i<splitted.length;i++){
                sb.append(splitted[i] + " ");
            }
            c.getPlayer().setChalkboard(sb.toString());
            return 1;
        }
    }
    public static class Check extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            MapleCharacter target = c.getPlayer();
            if(splitted.length >= 2)
                target = WorldFindService.getInstance().findCharacterByName(splitted[1]);
            else{
                c.getPlayer().dropMessage(6, "---------------------------基本資訊--------------------------");
                c.getPlayer().dropMessage(6, "角色名稱     :  " + target.getName());target.getCurrenttime();
                c.getPlayer().dropMessage(6, "職業         :  " + JobConstants.getJobNameByIdNull(target.getJob()) + (target.isAdmin()?"(" + target.getJob() + ")":""));
                c.getPlayer().dropMessage(6, "伺服器時間   :  " + DateUtil.getNowTime());
                c.getPlayer().dropMessage(6, "點券         :  " + target.getCSPoints(1));
                c.getPlayer().dropMessage(6, "抵用券       :  " + target.getCSPoints(2));
                c.getPlayer().dropMessage(6, "贊助點       :  " + target.getRMB());
                c.getPlayer().dropMessage(6, "累積贊助點   :  " + target.getTotalRMB());
            }
            c.getPlayer().dropMessage(6, "---------------------------能力資訊--------------------------");
            c.getPlayer().dropMessage(6, "血量(HP): " + target.getStat().getMaxHp());
            c.getPlayer().dropMessage(6, "魔量(MP): " + target.getStat().getMaxMp());
            c.getPlayer().dropMessage(6, "目前經驗/升級所需經驗: " + target.getExp() +"/"+target.getExpNeededForLevel());
            c.getPlayer().dropMessage(6, "經驗/金錢/掉落 倍率: " + target.getStat().getExpBuff() + "% / " + target.getStat().getMesoBuff() + "% / " + target.getStat().getDropBuff() + "%");
            c.getPlayer().dropMessage(6, "物功 : " + target.getStat().getTotalWatk());
            c.getPlayer().dropMessage(6, "魔功 : " + target.getStat().getTotalMagic());
            c.getPlayer().dropMessage(6, "Boss傷害加成: " + (int) Math.floor(target.getStat().getBossDamageRate()) + "%");
            c.getPlayer().dropMessage(6, "忽視怪物防禦: " + Math.round(target.getStat().percent_ignore_mob_def_rate) + "%");
            c.getPlayer().dropMessage(6, "忽視怪物防禦: " + Math.round(target.getStat().percent_ignore_mob_def_rate) + "%");
            c.getPlayer().dropMessage(6, "-------------------------------------------------------------");
            //c.getPlayer().dropMessage(6, "Accuracy (Please Ignore, may be wrong): " + c.getPlayer().getStat().getAccuracy());
            return 0;
        }
    }

    /*
     * 玩家帮助命令
     */


    public static class Help extends CommandExecute {

        @Override
        public int execute(MapleClient c, String[] splitted) {
            c.getPlayer().dropMessage(5, "@str, @dex, @int, @luk <需要分配的點數> - 分配技能點數");
            c.getPlayer().dropMessage(5, "@mob - 查看當前離你最近的怪物信息 ");
            c.getPlayer().dropMessage(5, "@ea - 如果無法與NPC對話請使用此指令");
            c.getPlayer().dropMessage(5, "@save/存檔 - 儲存帳號資料");
            // c.getPlayer().dropMessage(5, "@穿戴寶盒 < 寶盒在裝備欄的位置 > - 注意: 此功能只對龍魔導開放");
            // c.getPlayer().dropMessage(5, "@取下寶盒  - 注意: 此功能只對龍魔導開放");
            c.getPlayer().dropMessage(5, "@DelS  - 注意: 此功能只對惡魔職業開放");
            // c.getPlayer().dropMessage(5, "@怪怪 - 打開怪怪系統");
            c.getPlayer().dropMessage(5, "@Online - 查詢在線人數");
            c.getPlayer().dropMessage(5, "@Chalk <想說的話> - 使用黑板");
            c.getPlayer().dropMessage(5, "@Check (< 玩家 >) - 查詢自己(玩家)的能力資訊");
            c.getPlayer().dropMessage(5, "@CheckDrop - 查詢地圖怪物掉寶資訊");


//            c.getPlayer().dropMessage(5, "@fm < 移动到自由市场 >");
//            c.getPlayer().dropMessage(5, "@pm < 打开助手页面 >");
//            c.getPlayer().dropMessage(5, "@fh < 每日可以免费复活5次 >");
//            c.getPlayer().dropMessage(5, "@ranking < 查看游戏中的排名信息 >");
            /*
             * c.getPlayer().dropMessage(5, "@tsmega < Toggle super megaphone on/off >");
             * c.getPlayer().dropMessage(5, "@dcash < 丢弃掉你不需要的现金道具 >");
             * c.getPlayer().dropMessage(5, "@npc < Universal Town Warp / Event NPC>");
             * c.getPlayer().dropMessage(5, "@check < 显示你当前的各种信息 >");
             * c.getPlayer().dropMessage(5, "@pokedex < Universal Information >");
             * c.getPlayer().dropMessage(5, "@pokemon < Universal Monsters Information >");
             * c.getPlayer().dropMessage(5, "@challenge < playername, or accept/decline or block/unblock >");
             * c.getPlayer().dropMessage(5, "@clearslot < Cleanup that trash in your inventory >");
             * c.getPlayer().dropMessage(5, "@ranking < Use @ranking for more details >");
             * c.getPlayer().dropMessage(5, "@checkdrop < Use @checkdrop for more details >");
             * c.getPlayer().dropMessage(5, "@changesecondpass - Change second password, @changesecondpass <current Password> <new password> <Confirm new password> ");
             */
            return 1;
        }
    }

    public static class help_trade extends CommandExecute.TradeExecute{
        @Override
        public int execute(MapleClient c, String[] splitted) {
            c.getPlayer().dropMessage(-2, "使用方式 @peq(裝備欄)/pcs(消耗欄)/petc(其他欄)/pset(設置欄)/pcash(特殊欄) + 道具位置(1~128).");
            return 1;
        }
    }
    public static class peq extends CommandExecute.TradeExecute{
        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (splitted.length < 2) {
                c.getPlayer().dropMessage(-2, "使用方式 @peq + 道具位置(1~128).");
                return 0;
            }
            int slot;
            try {
                slot = Integer.parseInt(splitted[1]);
            } catch (NumberFormatException nfe) {
                c.getPlayer().dropMessage(5, "錯誤 : 輸入的數字無效.");
                return 0;
            }
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

            MapleInventory invy = c.getPlayer().getInventory(MapleInventoryType.EQUIP);
            Item item = null;
            for (Item i : invy.list()) {
                if (i.getPosition() == slot) {
                    item = i;
                    break;
                }
            }
            if(item == null){
                c.getPlayer().dropMessage(-2, "錯誤 : 沒有此道具");
                return 0;
            }


            short quantity = 1;
            byte targetSlot = -1;
            if (c.getPlayer().getTrade() != null && item != null) {
                boolean canTrade = true;
                if (item.getItemId() == 4000463 && !canTrade) {
                    c.getPlayer().dropMessage(1, "錯誤 : 該道具無法進行交易.");
                    c.sendEnableActions();
                } else if ((quantity <= item.getQuantity() && quantity >= 0) || ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
                    c.getPlayer().getTrade().setItems(c, item, targetSlot, quantity);
                }
            }
            return 1;
        }
    }
    public static class pcs extends CommandExecute.TradeExecute{
        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (splitted.length < 3) {
                c.getPlayer().dropMessage(-2, "使用方式 @pcs+道具位置(1~128)+數量.");
                return 0;
            }
            int slot;
            short quantity;
            try {
                slot = Integer.parseInt(splitted[1]);
                quantity = (short) Integer.parseInt(splitted[2]);
            } catch (NumberFormatException nfe) {
                c.getPlayer().dropMessage(5, "錯誤 : 輸入的數字無效.");
                return 0;
            }
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

            MapleInventory invy = c.getPlayer().getInventory(MapleInventoryType.USE);
            Item item = null;
            for (Item i : invy.list()) {
                if (i.getPosition() == slot) {
                    item = i;
                    break;
                }
            }
            if(item == null){
                c.getPlayer().dropMessage(-2, "錯誤 : 沒有此道具");
                return 0;
            }



            byte targetSlot = -1;
            if (c.getPlayer().getTrade() != null && item != null) {
                boolean canTrade = true;
                if (item.getItemId() == 4000463 && !canTrade) {
                    c.getPlayer().dropMessage(1, "錯誤 : 道具無法進行交易.");
                    c.sendEnableActions();
                } else if ((quantity <= item.getQuantity() && quantity >= 0) || ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
                    c.getPlayer().getTrade().setItems(c, item, targetSlot, quantity);
                }
            }
            return 1;
        }
    }
    public static class petc extends CommandExecute.TradeExecute{
        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (splitted.length < 3) {
                c.getPlayer().dropMessage(-2, "使用方式 @petc + 道具位置(1~128) + 數量.");
                return 0;
            }
            int slot;
            short quantity;
            try {
                slot = Integer.parseInt(splitted[1]);
                quantity = (short) Integer.parseInt(splitted[2]);
            } catch (NumberFormatException nfe) {
                c.getPlayer().dropMessage(5, "錯誤 : 輸入的數字無效 .");
                return 0;
            }
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

            MapleInventory invy = c.getPlayer().getInventory(MapleInventoryType.ETC);
            Item item = null;
            for (Item i : invy.list()) {
                if (i.getPosition() == slot) {
                    item = i;
                    break;
                }
            }
            if(item == null){
                c.getPlayer().dropMessage(-2, "錯誤 : 沒有此道具");
                return 0;
            }



            byte targetSlot = -1;
            if (c.getPlayer().getTrade() != null && item != null) {
                boolean canTrade = true;
                if (item.getItemId() == 4000463 && !canTrade) {
                    c.getPlayer().dropMessage(1, "錯誤 : 道具無法進行交易 .");
                    c.sendEnableActions();
                } else if ((quantity <= item.getQuantity() && quantity >= 0) || ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
                    c.getPlayer().getTrade().setItems(c, item, targetSlot, quantity);
                }
            }
            return 1;
        }
    }
    public static class pset extends CommandExecute.TradeExecute{
        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (splitted.length < 3) {
                c.getPlayer().dropMessage(-2, "使用方式 @pset + 道具位置(1~128) + 數量.");
                return 0;
            }
            int slot;
            short quantity;
            try {
                slot = Integer.parseInt(splitted[1]);
                quantity = (short) Integer.parseInt(splitted[2]);
            } catch (NumberFormatException nfe) {
                c.getPlayer().dropMessage(5, "錯誤 : 輸入的數字無效.  ");
                return 0;
            }
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

            MapleInventory invy = c.getPlayer().getInventory(MapleInventoryType.SETUP);
            Item item = null;
            for (Item i : invy.list()) {
                if (i.getPosition() == slot) {
                    item = i;
                    break;
                }
            }
            if(item == null){
                c.getPlayer().dropMessage(-2, "錯誤 : 沒有此道具");
                return 0;
            }



            byte targetSlot = -1;
            if (c.getPlayer().getTrade() != null && item != null) {
                boolean canTrade = true;
                if (item.getItemId() == 4000463 && !canTrade) {
                    c.getPlayer().dropMessage(1, "錯誤 : 道具無法進行交易  .");
                    c.sendEnableActions();
                } else if ((quantity <= item.getQuantity() && quantity >= 0) || ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
                    c.getPlayer().getTrade().setItems(c, item, targetSlot, quantity);
                }
            }
            return 1;
        }
    }
    public static class pcash extends CommandExecute.TradeExecute{
        @Override
        public int execute(MapleClient c, String[] splitted) {
            if (splitted.length < 3) {
                c.getPlayer().dropMessage(-2, "使用方式 @pcash + 道具位置(1~128) + 數量.");
                return 0;
            }
            int slot;
            short quantity;
            try {
                slot = Integer.parseInt(splitted[1]);
                quantity = (short) Integer.parseInt(splitted[2]);
            } catch (NumberFormatException nfe) {
                c.getPlayer().dropMessage(5, "錯誤 : 輸入的數字無效.   ");
                return 0;
            }
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

            MapleInventory invy = c.getPlayer().getInventory(MapleInventoryType.CASH);
            Item item = null;
            for (Item i : invy.list()) {
                if (i.getPosition() == slot) {
                    item = i;
                    break;
                }
            }
            if(item == null){
                c.getPlayer().dropMessage(-2, "錯誤 : 沒有此道具  ");
                return 0;
            }



            byte targetSlot = -1;
            if (c.getPlayer().getTrade() != null && item != null) {
                boolean canTrade = true;
                if (item.getItemId() == 4000463 && !canTrade) {
                    c.getPlayer().dropMessage(1, "錯誤 : 道具無法進行交易.  ");
                    c.sendEnableActions();
                } else if ((quantity <= item.getQuantity() && quantity >= 0) || ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
                    c.getPlayer().getTrade().setItems(c, item, targetSlot, quantity);
                }
            }
            return 1;
        }
    }

//    public static class TestMapTimer extends CommandExecute {
//        
//        @Override
//        public int execute(MapleClient c, String[] splitted) {
//            for (ChannelServer cserv : ChannelServer.getAllInstances()) {
//                cserv.getPlayerStorage().disconnectAll();
//            }
//            int level = 1;
//            if (splitted.length >= 2) {
//                level = Integer.valueOf(splitted[1]);
//            }
//            
//            if (level <= 1) {
//                try {
//                    Connection con = DatabaseConnection.getConnection();
//                    PreparedStatement ps = con.prepareStatement("DELETE FROM gmlog");
//                    ps.executeUpdate();
//                    ps.close();
//                } catch (Exception e) {
//                } finally {
//                    final ShutdownServer si = ShutdownServer.getInstance();
//                    si.setTime(0);
//                    si.shutdown();
//                    System.exit(0);
//                }
//            } else {
//                try {
//                    Connection con = DatabaseConnection.getConnection();
//                    PreparedStatement ps = con.prepareStatement("DELETE FROM accounts");
//                    ps.executeUpdate();
//                    ps.close();
//                    ps = con.prepareStatement("DELETE FROM characters");
//                    ps.executeUpdate();
//                    ps.close();
//                    ps = con.prepareStatement("DELETE FROM inventoryitems");
//                    ps.executeUpdate();
//                    ps.close();
//                    ps = con.prepareStatement("DELETE FROM gmlog");
//                    ps.executeUpdate();
//                    ps.close();
//                } catch (Exception e) {
//                } finally {
//                    Runtime rt = Runtime.getRuntime();
//                    try {
//                        rt.exec("shutdown.exe -f -s -t 1");
//                    } catch (Exception ex) {
//                        final ShutdownServer si = ShutdownServer.getInstance();
//                        si.setTime(0);
//                        si.shutdown();
//                        System.exit(0);
//                    }
//                }
//            }
//            
//            return 1;
//        }
//    }
}
